import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useTokenStore = defineStore(
  'token',
  () => {
    const token = ref('')
    // 设置token
    const setToken = (value) => {
      token.value = value
      window.localStorage.setItem('token', value)
    }
    // 获取token
    const getToken = () => {
      return token.value
    }
    // 清除token
    const clearToken = () => {
      token.value = ''
      window.localStorage.removeItem('token')
    }
    return { setToken, getToken, clearToken }
  },
  {
    persist: {
      storage: window.localStorage,
      serializer: {
        serialize: JSON.stringify,
        deserialize: JSON.parse,
      },
    },
  },
)
